Git Kata 2: Branches
Learn about listing, creating, and working with branches.
Branching is one of the most used and useful features of Git. Commits are always applied to a branch. Creating a new branch in a local repository starts a new commit history, starting from a specific commit in the source branch from which the new branch is created.
Branches are used to isolate work. New work, such as features and hotfixes, are created on a new local branch. These are referred to as feature or topic branches. Branches keep work organized and prevent new work from breaking existing work. Developers can create new local branches at will and commit changes to them. Branches can then be discarded or pushed to a remote server and merged into other branches.
Step 1: Listing and creating branches#
The command to list all the branches is given below.
The output will be something like this:
Command's Parameter
Command / Parameter | Description |
| When executed with no other parameters, this lists all the branches. |
The git branch command lists all the branches in the local repository. Git repositories are initialized with one branch, master (also being referred to as main in this chapter). The current branch is highlighted and prepended with a star.
The command to create a new branch is given below.
The result after executing the command will be something like this:
Command's Parameters
Command / Parameter | Description |
| This executes commands related to branches. |
| This is the name of the new branch to create. |
When combined with a branch name, the branch command creates a new branch. When successful, this command produces no output.
Note: The
git branch newbranchcommand creates a new branch, which is another type of ref. Remember that each commit is always applied to a branch. So far, all the commits have been applied tomain. Now that a new branch has been added, commits can be applied to either branch.
The command to list all the branches is given below.
The output will be something like this:
Command's Parameter
Command / Parameter | Description |
| When executed with no other parameters, this lists all the branches. |
Executing git branch again shows that the newbranch branch has been created, and that main is still the current branch.
Step 2: Switch between branches#
The command to switch to newbranch is given below.
When we run the command above, the output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This switches to a designated branch when combined with a branch name. |
| This indicates the branch to be checked out. |
The checkout command can act on two repository objects: commits and branches. This execution combines checkout with newbranch. This makes newbranch the current branch.
Note: Checkout is something quite different in CVCS systems such as Subversion, Clearcase, or Team Foundation Server. The
git checkoutcommand does something quite different: it checks out an object (a commit or a branch) from the local index and applies it to the working directory. Git doesn’t have the concept of an exclusive checkout.
The command to list all the branches is given below.
The result will be something like this:
Command's Parameter
Command / Parameter | Description |
| When executed with no other parameters, this lists all the branches. |
This command lists the local branches again. We now see that newbranch is the current (checked out) branch.
To swap lines in the file and save:
- Switch to the text editor.
- Swap the first and last lines of
storelist.txt. - Save the changes.
This step makes a new change to the storelist.txt file.
The command to get the repository status is given below.
The output of the command above will be something like this:
Command's Parameter
Command / Parameter | Description |
| This shows the status of all the files in the repository. |
The git status command shows the state of the repository. The first line shows that the current branch is newbranch. The change we just made to storelist.txt has not been staged or committed.
The status output also explains that we could, if desired, use the git checkout command to discard the changes in the working directory. Remember that git checkout is used to pull changes out of the index and apply them to the working directory. If we were to do that now, storelist.txt would be overwritten in the working directory and the change would be discarded. The storelist.txt file in the working directory would be updated to match what’s in the index. The output of this command is:
Output
Message | Meaning |
"On branch newbranch" | This is the current branch. |
"Changes not staged for commit: (use git add <file>...” to update what will be committed) (use git checkout --<file>... to discard changes in working directory)" | The list of changes below this message haven't been staged for commit. |
"modified: storelist.txt" | The red text indicates that change to |
"no changes added to commit (use git add and/or git commit -a)" | There are changes in the working directory that must be staged in order to be included in the next commit. |
Step 3: Commit to a branch#
The command to commit the changes to the repository is given below.
The output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This saves the current state of staged files in a new commit. |
| This stages all the modified files. |
| This designates a commit message on the command line. |
| This is the commit message associated with the new commit. |
This new commit is applied to newbranch.
The output of the command is:
Output
Message | Meaning |
"newbranch" | This is the branch to which the commit was applied. |
"1e19013" | These are the first seven characters of the commit hash. |
“First line last” | This is the commit message. |
"1 file changed, 2 insertions(+), 2 deletions(-) | This is the number of files changed and the number of lines inserted and deleted. |
Note: This is the first commit to a branch other than
main. The new commit has been applied tonewbranch. TheHEADref points to the new commit onnewbranch.
The command to switch to the main branch is given below.
After the execution of the command above, the output will be something like this:
Command's Parameters
Command / Parameter | Description |
| This checks out a branch or commit. |
| This is the name of the branch to checkout. |
This command switches the current branch back to main.
Note: For the first time,
HEADdoesn’t point to the most recent commit. It points to the last commit made on themainbranch. This is an example of how Git moves theHEADref to track where new commits will be added.
To switch to the text editor and reload:
- Switch to the text editor.
- Click “Reload.”
- Note that the file has changed.
The previous command, git checkout master, switched to the main branch. This checks out the main branch from the local index, which updates the working directory to the state saved in main. Returning to the text editor, we see that the storelist.txt has changed. The file is now back to its previous state, as it is in the main branch. The storelist.txt file now has two versions: one in main and another in newbranch. The next kata will demonstrate how the commits from one branch can be applied to another branch.
Practice commands#
We’ve given a terminal and a table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!
Commands
Step | Command |
This lists all the branches. |
|
This creates a new branch called |
|
This lists all the branches. |
|
This switches to |
|
This lists all the branches. |
|
This swaps the first and last lines of |
|
This gets the status of the repository. |
|
This commits the change to the repository, setting the commit comment from the command line. |
|
This switches to the |
|
This returns to the text editor and reloads the |
|
Git Kata 1: New Local Repository
Git Kata 3: Merging